﻿<%@ WebHandler Language="C#" Class="AnswerHandler" %>

using System;
using System.Collections.Generic;
using System.Web;

using EHSubmit.Util;
using EHSubmit.Logic;
using EHSubmit.Logic.LINQ;
using EHSubmit.Model;

public class AnswerHandler : EHSubmit.Logic.BaseAjaxHandler {

    protected override void Mapping()
    {
        OprationMap["addObjectiveWork"] = "AddObjectiveWork";                    //列出习题
    }

    public void AddObjectiveWork()
    {
        try
        {
            int goalId = int.Parse(Context.Request["goalId"]);
            
            QuestionManager quesManager = new QuestionManager();
            IList<Question> ques = quesManager.GetQuestionsWithoutOptions(goalId);
            
            // 此次答题的答案列表
            IList<Answer> ansers = new List<Answer>();
            
            
            int total = ques.Count;
            float rightCount = 0;
            string currAnswer = null;
            foreach(Question q in ques){
                currAnswer = Context.Request[q.QID.ToString()];

                // 允许某些题不做，系统将算作错误
                if (currAnswer == null)
                {
                    currAnswer = string.Empty;
                }
                
                currAnswer = currAnswer.Replace(",", string.Empty);
                Answer an = new Answer();
                an.OwnerAnswer = currAnswer;
                an.IsRight = false;
                an.OwnerID = CurrentUser.ID;
                an.QuestionID = q.QID;

                if (q.Answer == currAnswer)
                {
                    an.IsRight = true;
                    rightCount++;
                }
                else
                {
                    an.IsRight = false;
                }
                
                // 添加自己的答案
                ansers.Add(an);
            }

            //------------添加用户的答案，留作分析
            AnswerManager anserManager = new AnswerManager();
            anserManager.AddAnswers(ansers, goalId);
            

            //计算成绩
            int score = Convert.ToInt32(rightCount / total * 100);
            
            
            //-------------插入作业信息
            HomeworkManager hmManager = new HomeworkManager();
            Homework hmwork = new Homework();

            //hmwork.Description = Context.Request["description"];
            //hmwork.AttachPath = hFileName;         // 这里不将物理地址存入库,在列表时要通过JSON读AttachPath字段,使用物理地址可能不安全,将虚拟地址入库
            hmwork.GoalName = Context.Request["goalName"];
            hmwork.GoalID = goalId;
            hmwork.GoalType = 1;  // 客观题
            hmwork.StudentID = CurrentUser.ID;
            hmwork.SubmitTime = DateTime.Now;
            hmwork.UpdateTime = hmwork.SubmitTime;
            hmwork.Term = ConfigHelper.GetTerm();   // TODO   暂时根据配置文件获得当前学期，本应根据教务处同步，待处理。
            hmwork.Scores = score;
            
            hmManager.AddHomework(hmwork);

                        
            
            //-----------服务器应答
            IsSuccess = true;
            ResponseText = "{" + string.Format("success:true,msg:'答题结束，您的得分是：{0}'", score) + "}";
            
        }
        catch (Exception e)
        {
            LogHelper.Log(e.Message, e);
            Message = e.Message;
        }
    }


}